package com.starpl.mobile.android.monthunsae.november.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.platform.command.StarplCommand;
import com.starpl.mobile.android.platform.common.base.BaseActivity;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.common.network.HttpHandler;

public class ReportActivity extends BaseActivity implements OnClickListener
{
	private static final int CONTEXT_MENU_REPORT_AD = 0;
	private static final int CONTEXT_MENU_REPORT_PORN = 1;
	private static final int CONTEXT_MENU_REPORT_CUSS = 2;
	private static final int CONTEXT_MENU_REPORT_PLASTER = 3;
	
	private ImageButton mIbReportPost;
	private ImageButton mIbReportProfile;
	private ImageButton mIbReportNickName;
	private ImageButton mIbReportIntroduce;
	
	private String mCompleteMessage;
	private int mPostIdx;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_report_activity);
		
		mIbReportPost = (ImageButton) findViewById(R.id.comment_report_activity_ib_report_post);
		mIbReportProfile = (ImageButton) findViewById(R.id.comment_report_activity_ib_report_profile);
		mIbReportNickName = (ImageButton) findViewById(R.id.comment_report_activity_ib_report_nickname);
		mIbReportIntroduce = (ImageButton) findViewById(R.id.comment_report_activity_ib_report_introduce);
		
		registerForContextMenu(mIbReportPost);
		mIbReportPost.setOnClickListener(this);
		mIbReportProfile.setOnClickListener(this);
		mIbReportNickName.setOnClickListener(this);
		mIbReportIntroduce.setOnClickListener(this);
		
		mPostIdx = getIntent().getIntExtra("postIdx", 0);
		mCompleteMessage = "신고가 정상 접수되었습니다.\n감사합니다.";
	}

	/**
	 * 입력된 신고 종류에 맞게 글을 신고한다.
	 * 
	 * @param kind 신고종류( "profile" "nick_name" "title" )
	 */
	private void reportIllegalPost(String kind)
	{
		StarplCommand.reportIllegalPost(new HttpHandler(this)
		{
			@Override
			public void didSucceed(Object result)
			{
				toast(mCompleteMessage);
				finish();
			}
		}, mPostIdx, kind);
	}
	
	/**
	 * 정말 신고할것인지 묻는 팝업을 생성한다.
	 * 
	 * @param kind 신고종류( "profile" "nick_name" "title" )
	 */
	private void askReportDialog(final String kind)
	{
		DialogInterface.OnClickListener reportListener = new DialogInterface.OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int id)
			{
				reportIllegalPost(kind);
				dialog.dismiss();
			}
		};
		
		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int id)
			{
				dialog.dismiss();
			}
		};
		
		new AlertDialog.Builder(this)
			.setTitle(BaseApplication.getConfigPersistent("appsName"))
			.setMessage("정말 신고 하시겠습니까?")
			.setPositiveButton("신고", reportListener)
			.setNegativeButton("취소", cancelListener)
			.show();
	}
	
	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view)
	{
		switch(view.getId())
		{
		// 글 내용
		case R.id.comment_report_activity_ib_report_post:
			openContextMenu(view);
			break;
		
		// 프로필 사진
		case R.id.comment_report_activity_ib_report_profile:
			askReportDialog("profile");
			break;
		
		// 닉네임
		case R.id.comment_report_activity_ib_report_nickname:
			askReportDialog("nick_name");
			break;
			
		// 소개글
		case R.id.comment_report_activity_ib_report_introduce:
			askReportDialog("title");
			break;
		}
	}

	/** 
	 * 글을 신고할 것인지 묻는 팝업을 출력한다.
	 * 
	 * @param type 글신고 타입
	 */
	private void askPostReport(final String type)
	{
		DialogInterface.OnClickListener reportListener = new DialogInterface.OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int id)
			{
				StarplCommand.reportIllegalPost(new HttpHandler(ReportActivity.this)
				{
					@Override
					public void didSucceed(Object result)
					{
						toast(mCompleteMessage);
						finish();
					}
				}, mPostIdx, type);
				dialog.dismiss();
			}
		};
		
		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int id)
			{
				dialog.dismiss();
			}
		};
		
		new AlertDialog.Builder(this)
			.setTitle(BaseApplication.getConfigPersistent("appsName"))
			.setMessage("정말 신고 하시겠습니까?")
			.setPositiveButton("신고", reportListener)
			.setNegativeButton("취소", cancelListener)
			.show();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		String type = "etc";
		
		switch(item.getItemId())
		{
		case CONTEXT_MENU_REPORT_AD:
			type = "spam_ad";
			break;
			
		case CONTEXT_MENU_REPORT_PORN:
			type = "porno";
			break;
			
		case CONTEXT_MENU_REPORT_CUSS:
			type = "cuss";
			break;
			
		case CONTEXT_MENU_REPORT_PLASTER:
			type = "plaster";
			break;
		}
		
		askPostReport(type);
		
		return false;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.setHeaderTitle("신고 내용을 선택해 주세요.");
		menu.add(0, CONTEXT_MENU_REPORT_AD, 0, "광고/홍보");
		menu.add(0, CONTEXT_MENU_REPORT_PORN, 0, "음란/혐오");
		menu.add(0, CONTEXT_MENU_REPORT_CUSS, 0, "욕설/비방");
		menu.add(0, CONTEXT_MENU_REPORT_PLASTER, 0, "도배행위");
	}
}